package cn.itsource.minio.controller;

import cn.itsource.minio.service.MinioService;
import cn.itsource.util.AjaxResult;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("minio")
public class MinioController {

    @Autowired
    private MinioService minioService;


    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        return AjaxResult.ok(minioService.upload(file));
    }

    @DeleteMapping("/{objectName}")
    public AjaxResult delete(@PathVariable("objectName") String objectName) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        minioService.delete(objectName);
        return AjaxResult.ok();
    }
}
